package com.ilop.sthome.ui.activity.scene;

import androidx.recyclerview.widget.GridLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.InputFilterUtil;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.event.EventAnswerOK;
import com.ilop.sthome.mvp.contract.scene.SceneEditContract;
import com.ilop.sthome.mvp.present.scene.SceneEditPresenter;
import com.ilop.sthome.common.command.SiterCommand;
import com.ilop.sthome.ui.adapter.scene.ChoseColorAdapter;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivitySceneEditBinding;

/**
 * @author skygge
 * @date 2020-01-03.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：场景设置
 */
public class SceneEditActivity extends ContainLoginActivity<SceneEditPresenter, ActivitySceneEditBinding> implements SceneEditContract.IView {

    private int mSceneId;
    private String mSceneName;
    private String mDeviceName;
    private int mPosition = -1;
    private int mNewPosition;
    private ChoseColorAdapter mColorAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scene_edit;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
        mSceneId = getIntent().getIntExtra(CommonId.KEY_SCENE_ID, -1);
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new SceneEditPresenter(mContext, mDeviceName, mSceneId);
    }

    @Override
    protected void initView() {
        super.initView();
        InputFilterUtil.setEditTextInhibitInputSpaChat(mContext, mDBind.etSceneName);
        mColorAdapter = new ChoseColorAdapter(mContext);
        mDBind.sceneColorList.setLayoutManager(new GridLayoutManager(mContext, 5));
        mDBind.sceneColorList.setAdapter(mColorAdapter);
        if (mSceneId<3){
            mDBind.sceneDelete.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {
        super.initData();
        mPresent.refreshName();
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivSettingBack.setOnClickListener(view -> {
            String mName = mDBind.etSceneName.getText().toString();
            if (mPosition != mNewPosition || !mSceneName.equals(mName)){
                mPresent.whetherToSave(mName);
            }else {
                finish();
            }
        });

        mDBind.tvSceneSave.setOnClickListener(view -> {
            showProgressDialog();
            String mName = mDBind.etSceneName.getText().toString();
            mPresent.onSaveScene(mName);
        });

        mDBind.sceneDelete.setOnClickListener(view -> mPresent.deleteScene());
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.CHOOSE_SCENE_COLOR, Integer.class).observe(this, integer -> {
            mNewPosition = integer;
            mColorAdapter.setColor(integer);
            mPresent.setSceneColor(integer);
        });

        LiveDataBus.get().with(CommonId.KEY_ANSWER_OK, EventAnswerOK.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "initDataBus: answerOK");
                if(event.getData_str1().length()==9) {
                    dismissProgressDialog();
                    int cmd = Integer.parseInt(event.getData_str1().substring(0, 4), 16);
                    if (cmd == SiterCommand.INCREASE_SCENE) {
                        if (CommonId.KEY_SUCCESS.equals(event.getData_str2())) {
                            mPresent.onSaveSuccess();
                        }else {
                            showToast(getString(R.string.EE_AS_SEND_EMAIL_FOR_CODE4));
                        }
                    }else if (cmd == SiterCommand.DELETE_SCENE) {
                        if (CommonId.KEY_SUCCESS.equals(event.getData_str2())) {
                            mPresent.deleteSceneSuccess(mSceneId);
                            LiveDataBus.get().with(CommonId.DELETE_SCENE_SUCCESS).setValue(0);
                        } else {
                            showToast(getString(R.string.failed));
                        }
                    }
                }
            }
        });
    }

    @Override
    public void showSceneName(String name) {
        mSceneName = name;
        mDBind.etSceneName.setText(name);
        if (mSceneId<3){
            mDBind.etSceneName.setEnabled(false);
        }
    }

    @Override
    public void showSceneColor(int position) {
        this.mPosition = position;
        mNewPosition = position;
        mColorAdapter.setColor(mPosition);
    }

    @Override
    public void showToastMsg(String msg) {
        showToast(msg);
    }

    @Override
    public void showProgress() {
        showProgressDialog();
    }

    @Override
    public void finishActivity() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            String mName = mDBind.etSceneName.getText().toString();
            if (mPosition != mNewPosition || !mSceneName.equals(mName)){
                mPresent.whetherToSave(mName);
            }else {
                finish();
            }
            return true;
        }else {
            return super.onKeyDown(keyCode, event);
        }
    }
}
